﻿// basic.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "main.h"
#include "basic_form.h"
#include "tile_box_form.h"

enum ThreadId
{
	kThreadUI
};

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
#ifdef _DEBUG
	AllocConsole();
	FILE* fp = NULL;
	freopen_s(&fp, "CONOUT$", "w+t", stdout);
	wprintf_s(L"Command:\n%s\n\n", L"");
#endif
	// 创建主线程
	MainThread thread;

	// 执行主线程循环
	thread.RunOnCurrentThreadWithLoop(nbase::MessageLoop::kUIMessageLoop);

	return 0;
}

void MainThread::Init()
{
	nbase::ThreadManager::RegisterThread(kThreadUI);

	// 获取资源路径，初始化全局参数
	std::wstring sAppDir = nbase::win32::GetCurrentModuleDirectory();
#ifdef _DEBUG
	ui::GlobalManager::Startup(sAppDir + L"\\resources\\", ui::CreateControlCallback(), false, L"themes", L"lang\\zh_CN", L"public\\global.xml");
#else
	ui::GlobalManager::OpenResZip(sAppDir + L"\\resources\\themes\\themes.zip", "");
	ui::GlobalManager::Startup(L"", ui::CreateControlCallback(), false, L"", L"lang\\zh_CN", L"public\\global.xml");
#endif // DEBUG

	// 创建一个默认带有阴影的居中窗口
#if TEST_TILE_BOX
	TileBoxForm* window = new TileBoxForm();
	window->Create(NULL, BasicForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, 0, true);
#else
	BasicForm* window = new BasicForm();
	window->Create(NULL, BasicForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, 0, false);
#endif
	
	window->CenterWindow();
	window->ShowWindow();
}

void MainThread::Cleanup()
{
	ui::GlobalManager::Shutdown();
	SetThreadWasQuitProperly(true);
	nbase::ThreadManager::UnregisterThread();
}
